package com.example.administrator.szb.util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.DownIngBean;
import com.example.administrator.szb.bean.VersionBean;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.service.UpdateAppServices;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by stone on 2016/11/2.
 */

public class UpdateAppService {
    private  final String TAG = "UpdateAppService";
    public  boolean IS_OPEN = false;
    private  Activity  context;
    private View view;
    private  String apk_path=null;
    private boolean is_show_tips = false;

    public UpdateAppService(Activity context,View v) {
        this.context = context;
        this.is_show_tips = false;
        this.view=v;
    }

    public UpdateAppService( Activity context,View v,boolean is_show_tips) {
        this.is_show_tips = is_show_tips;
        this.context = context;
        this.view=v;
    }
    VersionBean versionBean;
    public  void checkVersion() {


        if (SDCardUtils.isSDCardMounted()) {
            Map<String,Object> params = new HashMap<>();
            params.put("version", AppUtils.getVersionCode(context));
            params.put("code", HttpUtil.getParam());
            params.put("app", "android");
            HttpUtils.post(null,URLAddress.URLBASE+URLAddress.URL_BB, params, new OnResultListener() {
                @Override
                public void onSuccess(int requestCode, String str) {
                     versionBean = JSON.parseObject(str,new TypeReference<VersionBean>(){});
                    if (AppUtils.getVersionCode(context)<versionBean.getName()){
                        //有新版本
                        if (!UpdateAppServices.isStart){
                            showPop("",versionBean.getUrl());
                        }

                    }else {
                        //没有更新

                       // showPop("",versionBean.getUrl());
                        if (is_show_tips){
                            Toasts.show(context,"已是最新版本");
                        }
                    }




                }

                @Override
                public void onFailure(int requestCode, String str) {

                }
            });

        } else {

            Toasts.show(context, "SD卡不可用", 1000);
        }

    }

public static boolean isDowning = false;
private void showPop(String msg, final String url){
    View popView = LayoutInflater.from(context).inflate(
            R.layout.versions, null);
    LinearLayout ll_container = popView.findViewById(R.id.ll_container);
    TextView textView = popView.findViewById(R.id.textView);
    //textView.setText(msg);
    Button button = popView.findViewById(R.id.button);
    final ImageView close = popView.findViewById(R.id.close);
    final PopupWindow popupWindow = new PopupWindow(popView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isDowning=true;
            popupWindow.dismiss();
            DownIngBean downIngBean = new DownIngBean();
            downIngBean.setProgress(0);
            downIngBean.setPath(null);
            downIngBean.setUrl(url);
            downIngBean.setIsFail(0);
            showPoping(downIngBean);
            Intent intent = new Intent(context, UpdateAppServices.class);
            intent.putExtra("url",url);
            context.startService(intent);
            Toasts.show(context,"开始下载");
        }
    });
    ll_container.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (versionBean.getIscompellent()!=0){
                popupWindow.dismiss();
            }
            return true;
        }
    });
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });
    setBackgroundAlpha(0.5f);//设置屏幕透明度

    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
    popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            setBackgroundAlpha(1.0f);
            if (versionBean.getIscompellent()==0&&!isDowning){
                FinishActivity.finish();
            }
        }
    });

}
     PopupWindow popupWindowing;
    NumberProgressBar progressBar;
    TextView bottom_tips;
    public void showPoping(final DownIngBean event){
        if (popupWindowing==null){
            View popView = LayoutInflater.from(context).inflate(
                    R.layout.downing, null);
            progressBar = popView.findViewById(R.id.number_progress_bar);
            bottom_tips = popView.findViewById(R.id.bottom_tips);
            LinearLayout ll_container = popView.findViewById(R.id.ll_container);
            popupWindowing = new PopupWindow(popView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


            ll_container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (versionBean.getIscompellent()!=0){
                        popupWindowing.dismiss();
                    }
                    return true;
                }
            });


            popupWindowing.setBackgroundDrawable(new BitmapDrawable());
            popupWindowing.setFocusable(true);// 点击空白处时，隐藏掉pop窗口

            popupWindowing.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    setBackgroundAlpha(1.0f);
                    if (versionBean.getIscompellent()==0&&isDowning){

                        if (event.getIsFail()!=9){
                            showPoping(event);
                        }
                    }
                }
            });
        }
        progressBar.setProgress(event.getProgress());
        if (!popupWindowing.isShowing()){
            setBackgroundAlpha(0.5f);//设置屏幕透明度
            popupWindowing.showAtLocation(view, Gravity.CENTER,0,0);
        }
        if (event.getProgress()==100||event.getIsFail()==9){
            popupWindowing.dismiss();
        }


    }


    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = (context).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }

}



